package com.caption.controller;

import com.caption.remote.PaymentRemote;
import com.caption.support.TxContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private PaymentRemote paymentRemote;

    @GetMapping("/create")
    public String createOrder(){
        String result = paymentRemote.pay();
        log.info("返回结果:{}",result);
        new Thread(()->{
            String txnId = ThreadContext.get("txnId");
            log.info("子线程1{}",txnId);
            String userId = TxContextHolder.getCurrentContext().get("userId");
            log.info("consumer userId:{}",userId);
            new Thread(()->{
                String txnId2 = ThreadContext.get("txnId");
                log.info("子线程2{}",txnId2);
            }).start();
        }).start();
        return result;
    }

}
